<template>
  <div>
    <van-nav-bar title="添加车牌" left-text="首页" right-text="退出" @click-left="onClickLeft" @click-right="onClickRight"/>
    <div style="margin-top:5rem;">
      <carNumber @getPlateLicense="getPlateLicense"></carNumber>
      <van-button type="info" style="width:90%; margin-top:20px;" @click="addCar">添加车牌</van-button>
    </div>
  </div>
</template>
<script>
import carNumber from '../components/car'
export default {
  components:{
    carNumber
  },
  data(){
    return{
      plateNo:''
    }
  },
  methods:{
    // 接收车牌号
    getPlateLicense(value){
      this.plateNo = value
    },
    // 添加车牌
    addCar(){
      let parmes = {
        plateNo: this.plateNo
      }
      this.$api.carplateAddApi(parmes).then(resp=>{
        if(resp.code == 0){
          this.$toast('添加成功')
          this.$router.push({
            name:'infor'
          })
        }else{
          this.$toast(resp.msg)
        }
      })
    },
    onClickLeft() {
      this.$router.push({
        name:'index'
      })
    },
    onClickRight() {
      if(localStorage.getItem('token')){
        this.$api.logout().then(resp=>{
          if(resp.code == 0){
            localStorage.removeItem('openId')
            localStorage.removeItem('token')
            this.$router.push({
              name:'login'
            })
          }
        })
      }else{
        this.$router.push({
          name:'login'
        })
      }
    },
  }
}
</script>
